package mod.luckymod.blocks;

import mod.luckymod.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

public class SmallSodiumBlock extends SodiumBlock {
    public static final IntegerProperty COUNT = IntegerProperty.create("count", 1, 8);
    // 定义8个不同堆叠状态的形状（每个小钠块占1/8体积）
    private static final VoxelShape[] SHAPES = new VoxelShape[8];
    static {
        // 文档1: 单个方块在左下角 [0,0,0] 到 [8,8,8]
        SHAPES[0] = Block.box(0, 0, 0, 8, 8, 8);

        // 文档2: 添加第二个方块 [8,0,0] 到 [16,8,8]
        SHAPES[1] = Shapes.or(
                SHAPES[0],
                Block.box(8, 0, 0, 16, 8, 8)
        );

        // 文档3: 添加第三个方块 [8,0,8] 到 [16,8,16]
        SHAPES[2] = Shapes.or(
                SHAPES[1],
                Block.box(8, 0, 8, 16, 8, 16)
        );

        // 文档4: 添加第四方块 [0,0,8] 到 [8,8,16]
        SHAPES[3] = Shapes.or(
                SHAPES[2],
                Block.box(0, 0, 8, 8, 8, 16)
        );

        // 文档5: 添加第五个方块 [8,8,8] 到 [16,16,16]
        SHAPES[4] = Shapes.or(
                SHAPES[3],
                Block.box(8, 8, 8, 16, 16, 16)
        );

        // 文档6: 添加第六个方块 [8,8,0] 到 [16,16,8]
        SHAPES[5] = Shapes.or(
                SHAPES[4],
                Block.box(8, 8, 0, 16, 16, 8)
        );

        // 文档7: 添加第七个方块 [0,8,0] 到 [8,16,8]
        SHAPES[6] = Shapes.or(
                SHAPES[5],
                Block.box(0, 8, 0, 8, 16, 8)
        );

        // 文档8: 添加第八个方块 [0,8,8] 到 [8,16,16]
        SHAPES[7] = Shapes.or(
                SHAPES[6],
                Block.box(0, 8, 8, 8, 16, 16)
        );
    }

    public SmallSodiumBlock(Properties properties) {
        super(properties);
        this.registerDefaultState(this.stateDefinition.any().setValue(COUNT, 1));
        this.Splitting = false;
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(COUNT);
    }

    @Override
    public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
        int count = state.getValue(COUNT);
        return SHAPES[count - 1];
    }

    @Override
    public boolean canBeReplaced(BlockState state, BlockPlaceContext context) {
        // 只有当堆叠数量小于8时，才允许替换
        return context.getItemInHand().getItem() == this.asItem() && state.getValue(COUNT) < 8;
    }

    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        BlockPos pos = context.getClickedPos();
        Level level = context.getLevel();
        BlockState existingState = level.getBlockState(pos);

        // 如果当前位置已经是小钠块，增加堆叠数量
        if (existingState.getBlock() instanceof SmallSodiumBlock) {
            int currentCount = existingState.getValue(COUNT);

            if (currentCount < 8) {
                // 当放置第8个小钠块时，返回完整钠方块
                if (currentCount == 7) {
                    return ModBlocks.SODIUM_BLOCK.get().defaultBlockState();
                }
                // 增加堆叠数量
                return existingState.setValue(COUNT, currentCount + 1);
            }
            // 如果已经堆叠了8个，直接返回完整钠方块
            return ModBlocks.SODIUM_BLOCK.get().defaultBlockState();
        }
        // 如果当前位置不是小钠块，放置新的小钠块
        return this.defaultBlockState();
    }
}